<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0" />
      <el-col :span="12" :xs="24">
        <el-form
          ref="loginFroms"
          :model="loginForm"
          :rules="rules"
          class="login-form"
        >
          <h1>Hello</h1>
          <h2>欢迎来到我的世界</h2>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              :prefix-icon="user"
              placeholder="用户名"
              clearable
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              :prefix-icon="loack"
              type="password"
              placeholder="密码"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTimeMessage } from '@/utils/time'

const user = User
const loack = Lock
const useStore = useUserStore()

// 获取路由器
const $router = useRouter()
// 当前路由
const $route = useRoute()

const loading = ref(false)

const loginForm = reactive({
  username: '一路向东',
  password: '111111',
})

// 自定义规则
const validateUsername = (rule: any, value: any, callback: any) => {
  // rule 表单校验规则 value 表单元素内容 callback 函数：符合条件 校验通过调用
  if (/^[\u4e00-\u9fa5A-Za-z0-9]{4,10}$/.test(value)) {
    callback()
  } else {
    callback(new Error('账号长度为4-10位'))
  }
}
const validatePassword = (rule: any, value: any, callback: any) => {
  if (/^\d{6,10}$/.test(value)) {
    callback()
  } else {
    callback(new Error('密码长度为5-10位数字'))
  }
}

const loginFroms = ref()
// 表单校验规则
const rules = {
  // username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  username: [{ validator: validateUsername, trigger: 'blur' }],
  password: [{ validator: validatePassword, trigger: 'blur' }],
}

const login = () => {
  // 校验表单
  loginFroms.value.validate((valid) => {
    if (valid) {
      loading.value = true
      useStore
        .userLogin(loginForm)
        .then(() => {
          // 判断当前路由是否有 query 参数 如果有则跳转到 query，没有则跳转到首页
          const redirect = $route.query.redirect
          $router.push({ path: (redirect as string) || '/' })
          // 成功提示
          ElNotification({
            type: 'success',
            message: '欢迎回来',
            title: `Hi,${getTimeMessage()}好`,
          })
        })
        .catch((error) => {
          ElNotification({
            type: 'error',
            message: error.message,
          })
        })
        .finally(() => {
          loading.value = false
        })
    }
  })
}
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .login-form {
    position: relative;
    width: 50%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;
    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
